<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>事件委托</title>

    <style>
        .color {
            color: red;
        }
    </style>
</head>

<body>

    <ol>
        <li data-name="red" id="child1" class="red">lorem01red</li>
        <li data-name="blue">lorem02blue</li>
        <li data-name="green" class="green">lorem03green</li>
        <li>lorem04</li>
        <li>lorem05</li>
    </ol>


    <script>

        let ol = document.getElementsByTagName('ol')[0];

        ol.addEventListener('click', function (event) {
            // if(event.target.nodeName==='LI'){
            //     event.target.style.color='blue'
            // }
            let target = event.target;
            // if (target.dataset.name=='red'){
            //     target.style.color='red'
            // }


            if(target.className=='red'){
                target.style.color='red'
            }else if(target.className=='green'){
                target.style.color='green'
            }
        })




    </script>

</body>

</html>